#include <SDL/SDL.h>
#include "csockmain.h"
#include "cmenu.h"

//existing vector
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while(std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}

//new vector
std::vector<std::string> split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    return split(s, delim, elems);
}


int main(void)
{

if(SDL_Init(0)==-1)
    {
    //cout << "SDLNet_TCP_INIT: " << endl << SDLNet_GetError(); //Starts SDL
    exit(1);//On SDL error, exit with return value 1
    }

cSockmain NET;//NET main instance


NET.OpenSocket("127.0.0.1",55555);//Connects to host
NET.Connected = true;
NET.UID=1000000;
NET.StartThreads(); //Starts NET client thread

std::vector<std::string> data;

cout << "Client started succesfully" << endl << endl;
cout << "RPG TEXT CLIENT" << endl << endl;
cout << "USER ACCOUNT SELECTION" << endl;


int option;
cMenu myMenu;
myMenu.Clear();
myMenu.AddItem("-Login with an existing account");
myMenu.AddItem("-Create a new user account");
myMenu.AddItem("-exit game");

std::string fullname,username,password,password2;


bool logedin =false;
bool reply = false;
while (!logedin)
{
option=myMenu.GetItem();

if (option==0)//Login
    {
    cout << "ACCOUNT LOGIN" << endl;
    cout << "e-mail?: ";
    cin >> username;
    cout << "Password?: ";
    cin >> password;
    std::stringstream Msg;
    Msg << username << "#" << password << "#";
    NET.SockSend(LOGIN_WITH_ACCOUNT,Msg.str().c_str());
    }else if (option==1)//New account
    {
    bool done=false;
    while(!done)
        {
        cout << "NEW ACCOUNT CREATION" << endl;
        cout << "Full name?: ";
        cin  >> fullname;
        cout << "e-mail?: ";
        cin  >> username;
        cout << "Password?: ";
        cin  >> password;
        cout << "Repeat Password?: ";
        cin  >> password2;

        if(password.compare(password2)==0)
            {
                done=true;
            }else{
                cout << "Passwords don't match" << endl;
            }
        }

        std::stringstream Msg;
        Msg << fullname << "#"<< username << "#" << password << "#";
        NET.SockSend(CREATE_NEW_ACCOUNT,Msg.str().c_str());
        }else if (option==2)
        {
            return 0;//exit game
        }

cout << "Waiting for server reply" << endl;
while (!reply)
{
  if (NET.InputQueue.size()> 0)//queue has messages
    {
        std::string text;
        text.clear();
        SDL_SemWait( NET.QueueInLock );
        text = NET.InputQueue.front();//take a message out from the queue
        NET.InputQueue.pop();
        SDL_SemPost( NET.QueueInLock );
        data.clear();
        split(text,'/',data);//I split it into strings
        Uint16 command = atoi(data[1].c_str());

        switch(command)
            {
            case ACCOUNT_ACCEPTED:
            cout << "ACCOUNT_ACCEPTED" << endl;
            reply = true;
            logedin = true;
            break;

            case NEW_ACCOUNT_ACCEPTED:
            cout << "NEW_ACCOUNT_ACCEPTED" << endl;
            reply = true;
            break;

            case DUPLICATE_ACCOUNT :
            cout << "DUPLICATE_ACCOUNT " << endl;
            reply = true;
            break;

            case UNKNOW_USERNAME:
            cout << "UNKNOW_USERNAME" << endl;
            reply=true;
            break;

            case BAD_PASSWORD:
            cout << "BAD_PASSWORD" << endl;
            reply=true;
            break;
            }//switch
        }//Inputqueue > 0
    SDL_Delay(1000);
    if (NET.Connected == false ) return 2;
    }//while reply
    reply=false;
}//while logedin

cout << endl << "Welcome to socket server!" << endl << "You are now loged in" << endl;

SDL_Quit();//Exit SDL

    return 0;
}
